//
//  WFVisitorView.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/9.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
import SnapKit

class WFVisitorView: UIView {
    // MARK:  - 定义登陆注册按钮点击的闭包
    var loginOrRegisterClosure:(()->())?
    
    // MARK:  - 懒加载控件
    //旋转视图
    private lazy var cycleImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var coverImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页的小房子
    private lazy var homeImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

        return button
    }()
    
    //登陆按钮
    private lazy var loginBtn:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setTitle("登陆", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    //消息内容
    private lazy var messLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.numberOfLines = 0
        
        
        return label
    
    }()
    
    // MARK:  -  重写init方法,这里是入口
    override init(frame: CGRect) {
        super.init(frame: frame)
            setUpUI()
    }
    //如果使用xib就会进行提示
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  - 初始化控件
    func setUpUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1.0)
        //添加所有的控件
        addSubview(cycleImgView)
        addSubview(coverImgView)
        addSubview(homeImgView)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(messLabel)
        
        //手动添加约束
        cycleImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(center)
        }
        coverImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImgView)
        }
        homeImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImgView)
        }
        messLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImgView.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(centerX)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messLabel.snp_left)
            make.top.equalTo(messLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messLabel.snp_right)
            make.top.equalTo(registerBtn)
            make.size.equalTo(registerBtn)
        }
        
    }
    
    // MARK:  - 根据不同的视图加载不同的访客视图
    func loadVisitorinfo(message:String?,imgName:String?){
        if let msg = message , img = imgName {
            //两者都传入了值,说明不是首页
            messLabel.text = msg
            homeImgView.image = UIImage(named: img)
            cycleImgView.hidden = true
        }else{
            //传入的是空,说明是首页
            cycleImgView.hidden = false
            startAnimation()
        }
    }
    
    // MARK:  - 开启光环视图的旋转动画
    @objc private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImgView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    // MARK:  - 注册登陆的点击事件
    @objc private func registerBtnClick(){
        print("注册被点击了")
        loginOrRegisterClosure?()
    }
    @objc private func loginBtnClick(){
        print("登陆被点击了")
        loginOrRegisterClosure?()
    }
}
